﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text.RegularExpressions;

namespace Jy123.Web.PersonalInfo
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AlterPassword : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;
            string txtOldPsw = context.Request["txtOldPsw"];
            string txtPsw = context.Request["txtPsw"];
            string txtPsw2 = context.Request["txtPsw2"];
            string pswRegex = @"^(\w){6,20}$";
            if (CheckOldPsw(userID, txtOldPsw))
            {
                if (Regex.IsMatch(txtPsw, pswRegex) && Regex.IsMatch(txtPsw2, pswRegex))
                {
                    if (Equals(txtPsw, txtPsw2))
                    {
                        Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
                        mUser = bllUser.GetModel(userID);
                        mUser.Password = Jy123.Common.Input.MD5(txtPsw, false);
                        bllUser.Update(mUser);
                        msg = "修改成功,ok";
                    }
                    else
                    {
                        msg = "两次密码不一致,error";
                    }
                }
                else
                {
                    msg = "只能输入6-20个字母/数字/下划线";
                }
            }
            else
            {
                msg = "原密码不正确,error";
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(msg);
        }
        /// <summary>
        /// 检查原密码是否正确
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="psw">原密码</param>
        /// <returns>true/false</returns>
        private bool CheckOldPsw(int userID, string psw)
        {
            Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            mUser = bllUser.GetModel(userID);
            string oldPsw =Jy123.Common.Input.MD5(psw,false);
            if (Equals(mUser.Password,oldPsw))
            {
                return true;
            }
            return false;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
